package 旗下基金;

import Utils.DbTools;

import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.UpdateOptions;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
    static final String CompanyInfoFilePath = "D:\\测试临时文件\\choice抓取\\基金公司基本资料.xls";
    static MongoClient mongoClient = new MongoClient("192.168.1.173", 27017);
    static MongoDatabase mDatabase = mongoClient.getDatabase("基金choice抓取");
    static MongoCollection mCollection = mDatabase.getCollection("旗下基金");

    public static void main(String[] args) throws IOException, InvalidFormatException {
        List<Map<String, String>> companyList = getCompanyNames();
        for (int i = 0; i < 180; i++) {
            File excelFile = new File("D:\\测试临时文件\\choice抓取\\旗下基金\\"+(i+1) + ".xls");

            if (!excelFile.exists())
                continue;

            Workbook workbook = WorkbookFactory.create(excelFile);
            Sheet sheet = workbook.getSheetAt(0);
            List<Map<String, String>> docList = getDocList(sheet);

            String companyName = companyList.get(i).get("companyName");

            docList.forEach(doc -> {
                doc.put("基金公司名称",companyName);
                doc.put("_id", doc.get("基金公司名称") + "_" + doc.get("基金代码")+"_"+doc.get("基金名称")+"_"+
                        doc.get("基金类型")+"_"+doc.get("基金经理")+"_"+doc.get("基金成立日期"));
                DbTools.updateOne(mCollection, doc);
            });
        }
    }

    public static List<Map<String, String>> getDocList(Sheet sheet){
        List<Map<String, String>> resList = new ArrayList<>();
        int row_index = 1;
        while (sheet.getRow(row_index) != null){
            Map<String, String> doc = new HashMap<>();
            Row row = sheet.getRow(row_index);

            doc.put("基金代码", row.getCell(0)+"");
            doc.put("基金名称", row.getCell(1) + "");
            doc.put("基金类型", row.getCell(2) + "");
            doc.put("基金经理", row.getCell(10) + "");
            doc.put("基金成立日期", row.getCell(11) + "");

            resList.add(doc);
            row_index++;
        }
        return resList;
    }

    public static List<Map<String, String>> getCompanyNames() throws IOException, InvalidFormatException {
        Workbook workbook = WorkbookFactory.create(new File(CompanyInfoFilePath));
        Sheet sheet = workbook.getSheetAt(0);

        List<Map<String, String>> resList = new ArrayList<>();
        for (int i = 1;i < 180; i++) {
            Map<String, String> item = new HashMap<>();
            item.put("companyName", sheet.getRow(i).getCell(0).getStringCellValue());
            item.put("companyId", String.valueOf(i));
            item.put("成立日期", sheet.getRow(i).getCell(1).getStringCellValue());
            resList.add(item);
        }

        return resList;
    }
}
